<?php 
namespace Admin\Model;
use Think\Model;
class MdModel extends Model{
	protected  $tableName ="model";
	//验证
	protected $patchValidate = true;
	//添加模型
	public function d(){
		if($this->create()){
			$addModel=$this->add($this->getError());
			$m_table_name=I('m_table_name');
			if($addModel){
				//新建表
				$sql="CREATE TABLE d_$m_table_name SELECT * FROM d_content";
				M()->query($sql);
				$sql2="alter table d_$m_table_name change contid contid int unsigned not Null primary key auto_increment ";
				M()->query($sql2);
				//复制字段
				$sql3="INSERT INTO d_filed (name,f_fild,f_type,t_name,mid) VALUES ('标题','title','title','$m_table_name','$addModel'),('栏目','cid','cid','$m_table_name','$addModel'),('属性','flag','flag','$m_table_name','$addModel'),('SEO标题','seo_title','input','$m_table_name','$addModel'),('编辑器','edit','editor','$m_table_name','$addModel'),('正文','content','content','$m_table_name','$addModel'),('tag','tag','tag','$m_table_name','$addModel'),('关键字','keywords','input','$m_table_name','$addModel'),('描述','description','textarea','$m_table_name','$addModel'),('模板','template','template','$m_table_name','$addModel'),('缩略图','thumb','thoumb','$m_table_name','$addModel'),('点击','click','input','$m_table_name','$addModel'),('添加时间','addtime','datetime','$m_table_name','$addModel')"; 
				M()->query($sql3);
				return $this->updateCache();
					
			}else{
				return false;
			}	
		}else{
			return false;
		}
	}
	//修改模型
	public function edit(){
		if($this->create()){
			 $saveModel=$this->save();
			 if($saveModel){
				return $this->updateCache();
			 }
		}else{
			return false;
		}
	}
	public function dorp(){
		if(IS_POST){
			$mid=I('mid');
			$m_table_name=I('m_table_name');
			$sql="DROP TABLE d_{$m_table_name}";
			M()->query($sql);
			$del=$this->delete($mid);
			M("filed")->where("mid=$mid")->delete();
			$this->updateCache();
			return $del;
		}else{
			return false;
		}
	}
	//缓存
	public function updateCache(){
		S('m',null);
		$ModelData = $this->select();
		if(empty($ModelData)){
			return true;
		}
		$cache=S('m',$ModelData);
		if($cache){
			return true;
		}else{
			return false;
		}

	}
	

	


}

